package com.java.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.auth.mapper.SysRoleMapper;
import com.java.auth.mapper.SysUserRoleMapper;
import com.java.auth.service.SysRoleService;
import com.java.common.utils.R;
import com.java.model.system.SysRole;
import com.java.model.system.SysUserRole;
import com.java.vo.system.AssginRoleVo;
import com.java.vo.system.SysRoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper,SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public List<SysRole> findAll() {
        
        return null;
    }

    @Override
    public R pageSelectByLike(IPage<SysRole> iPage, SysRoleQueryVo sysRoleQueryVo) {

        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(sysRoleQueryVo.getRoleName())){
            queryWrapper.like("role_name",sysRoleQueryVo.getRoleName());
        }
        iPage = sysRoleMapper.selectPage(iPage, queryWrapper);
        R r = R.ok(iPage);
        return r;
    }

    /**
     * 根据用户获取角色数据
     * @param userId
     * @return
     */

    @Override
    public R findRoleById(Long userId) {
        //获取所有角色
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        //获取用户所拥有的角色id
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(queryWrapper);
        List<Long> collect = sysUserRoles.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
        //根据角色id，将角色封装到集合中
        List<SysRole> sysRoleList = new ArrayList<>();

        for (SysRole sysRole : sysRoles) {
            if(collect.contains(sysRole.getId())){
                sysRoleList.add(sysRole);
            }
        }
        //封装返回结果
        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assginRoleList", sysRoleList);
        roleMap.put("allRolesList", sysRoles);

        return R.ok(roleMap);
    }

    /**
     * 分配角色
     *
     * @param assginRoleVo
     * @return
     */
    @Transactional
    @Override
    public R doAssign(AssginRoleVo assginRoleVo) {
        //根据userId删除所有角色
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",assginRoleVo.getUserId());
        int rows = sysUserRoleMapper.delete(queryWrapper);
        for(Long roleId : assginRoleVo.getRoleIdList()) {
            if(StringUtils.isEmpty(roleId)) continue;
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(assginRoleVo.getUserId());
            userRole.setRoleId(roleId);
            sysUserRoleMapper.insert(userRole);
        }
        return R.ok();
    }
}
